﻿package chapter_03_Homework;

import java.util.Scanner;

public class Work_StudentsBiz {
	String[] names = new String[30];

	/**
	 * 增加学生姓名
	 * @param name	增加的姓名
	 */
	public void addName(String name){
		for (int i = 0; i < names.length; i++) {
			if(names[i]==null){
				names[i] = name;
				break;
			}
		}
	}

	/**
	 * 		显示学生姓名。
	 */
	public void showName(){
		System.out.println("本班学生列表：");
		for (int i = 0; i < names.length; i++) {
			if(names[i]!=null){
				System.out.println(names[i]+" ");
			}
		}
		System.out.println();
	}

	/**
	 * 		修改学生姓名。
	 * @param oldName		旧名字
	 * @param newName		新名字
	 * @return
	 */
	public boolean editName(String oldName,String newName){
		boolean isFind = false;
		for (int i = 0; i < names.length; i++) {
			if(names[i].equals(oldName)){
				names[i] = newName;
				isFind = true;
				break;
			}
		}
		return isFind;
	}
	/**
	 * 		在一定区间内查找学生姓名
	 * @param start		开始位置
	 * @param end		结束位置
	 * @param name	查找的姓名
	 * @return				是否查找成功
	 */
	public boolean searchName(int start,int end,String name){
		boolean isFind = false;
		for (int i = start-1; i < end; i++) {
			if(names[i].equals(name)){
				isFind = true;
				break;
			}
		}
		return isFind;
	}

	/**
	 * 		求平均分
	 * @param scores		参赛成绩数组
	 * @return
	 */
	public double calAvg(double[] scores){
		double sum =0.0;
		double avg = 0.0;
		for (int i = 0; i < scores.length; i++) {
			sum += scores[i];
		}
		avg = sum/scores.length;
		return avg;
	}

	/**
	 * 		求最高分
	 * @param scores		参赛成绩数组
	 * @return
	 */
	public double calMax(double[] scores){
		double max = scores[0];
		for (int i = 1; i < scores.length; i++) {
			if(max<scores[i])
				max = scores[i];
		}
		return max;
	}

	public static void main(String[] args) {

		Work_StudentsBiz st = new Work_StudentsBiz();
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < 5; i++) {
			System.out.print("请输入学生姓名：");
			String newName = sc.next();
			st.addName(newName);
		}
		st.showName();
		System.out.print("\n请输入开始查找的位置：");
		int s = sc.nextInt();
		System.out.print("\n请输入结束查找的位置：");
		int e = sc.nextInt();
		System.out.print("请输入查找的姓名：");
		String name = sc.next();
		System.out.println("\n******查找结果******");
		if(st.searchName(s, e, name)){
			System.out.println("找到了！");
		}else{
			System.out.println("没找到！");
		}
		double[] scores = new double[5];
		System.out.println("请输入五名参赛者的成绩：");
		for (int i = 0; i < 5; i++) {
			String string = args[i];
			scores[i] = sc.nextDouble();
		}
		double avgScore = st.calAvg(scores);
		System.out.println("平均成绩是："+avgScore);

		double maxScore = st.calMax(scores);
		System.out.println("最高成绩是："+maxScore);
	}
}
